package city.spring.modules.medicalstation.drugwarehouse.controller;

import city.spring.modules.medicalstation.drugwarehouse.entity.DrugwarehouseEntity;
import city.spring.modules.medicalstation.drugwarehouse.service.DrugwarehouseService;
import city.spring.modules.medicalstation.drugwarehouse.vo.DrugwarehouseVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Controller：医护站药品入库
 *
 * @author 谢金婷
 * @date 2020-09-21 17:00:56
 */
@RestController
@RequestMapping("/drugwarehouse")
public class DrugwarehouseController extends ApiController {
    private final DrugwarehouseService drugwarehouseService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<DrugwarehouseEntity, ?>> entityOrderFields;

    public DrugwarehouseController(DrugwarehouseService drugwarehouseService) {
        this.drugwarehouseService = drugwarehouseService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(DrugwarehouseEntity::getId);
        entityOrderFields.add(DrugwarehouseEntity::getDeliveryId);
        entityOrderFields.add(DrugwarehouseEntity::getMedicineId);
        entityOrderFields.add(DrugwarehouseEntity::getWarehousingmethod);
        entityOrderFields.add(DrugwarehouseEntity::getWarehousingdate);
        entityOrderFields.add(DrugwarehouseEntity::getUsersId);
        entityOrderFields.add(DrugwarehouseEntity::getApprovalStatus);
        entityOrderFields.add(DrugwarehouseEntity::getReviewer);
        entityOrderFields.add(DrugwarehouseEntity::getGmtCreate);
        entityOrderFields.add(DrugwarehouseEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>医护站药品入库</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, DrugwarehouseEntity entity) {
        LambdaQueryChainWrapper<DrugwarehouseEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>医护站药品入库</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault @SortDefault.SortDefaults({@SortDefault(sort = "id", direction = Sort.Direction.DESC)}) Pageable pageable, DrugwarehouseVO entity, Date[] warehousingdate) {
//        LambdaQueryChainWrapper<DrugwarehouseEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        Page<DrugwarehouseEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        return success(page);
        QueryWrapper<? extends DrugwarehouseEntity> queryWrapper = buildQuery(entity);
        if (warehousingdate != null && warehousingdate.length == 2) {
            queryWrapper.ge("t1.warehousingdate", warehousingdate[0]);
            queryWrapper.le("t1.warehousingdate", warehousingdate[1]);
        }
        return success(drugwarehouseService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 分页获取 <strong>医护站药品入库</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping("/otherlist")
    public Object otherlist(@PageableDefault @SortDefault.SortDefaults({@SortDefault(sort = "id", direction = Sort.Direction.DESC)}) Pageable pageable, DrugwarehouseVO entity, Date[] warehousingdate) {
//        LambdaQueryChainWrapper<DrugwarehouseEntity> lambdaQuery = buildLambdaQuery(entity);
//        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
//        Page<DrugwarehouseEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
//        return success(page);
        QueryWrapper<? extends DrugwarehouseEntity> queryWrapper = buildQuery(entity);
        if (warehousingdate != null && warehousingdate.length == 2) {
            queryWrapper.ge("t1.warehousingdate", warehousingdate[0]);
            queryWrapper.le("t1.warehousingdate", warehousingdate[1]);
        }
        return success(drugwarehouseService.otherlistPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }


    /**
     * 获取一个 <strong>医护站药品入库</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable Integer id) {
//        return success(drugwarehouseService.getById(id));
        QueryWrapper<DrugwarehouseVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        List<DrugwarehouseVO> records = drugwarehouseService.listPage(new Page<>(), wrapper).getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 获取一个 <strong>医护站药品入库</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("otherlist/{id}")
    public Object otherinfo(@PathVariable Integer id) {
//        return success(drugwarehouseService.getById(id));
        QueryWrapper<DrugwarehouseVO> wrapper = new QueryWrapper<>();
        wrapper.eq("t1.id", id);
        List<DrugwarehouseVO> records = drugwarehouseService.otherlistPage(new Page<>(), wrapper).getRecords();
        if (records.isEmpty()) {
            return success(null);
        }
        return success(records.get(0));
    }

    /**
     * 添加一个 <strong>医护站药品入库</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody DrugwarehouseEntity entity) {
        drugwarehouseService.saveDrugwarehouse(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>医护站药品入库</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable Integer id, @RequestBody DrugwarehouseEntity entity) {
        entity.setId(id);
        drugwarehouseService.updateDrugwarehouse(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>医护站药品入库</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable Integer id) {
        drugwarehouseService.deleteDrugwarehouse(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>医护站药品入库</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<Integer> ids) {
        drugwarehouseService.deleteDrugwarehouse(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<DrugwarehouseEntity> buildLambdaQuery(DrugwarehouseEntity entity) {
        LambdaQueryChainWrapper<DrugwarehouseEntity> lambdaQuery = drugwarehouseService.lambdaQuery();
        lambdaQuery.eq(entity.getId() != null, DrugwarehouseEntity::getId, entity.getId());
        lambdaQuery.eq(entity.getDeliveryId() != null, DrugwarehouseEntity::getDeliveryId, entity.getDeliveryId());
        lambdaQuery.eq(entity.getMedicineId() != null, DrugwarehouseEntity::getMedicineId, entity.getMedicineId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getWarehousingmethod()), DrugwarehouseEntity::getWarehousingmethod, entity.getWarehousingmethod());
        lambdaQuery.eq(entity.getWarehousingdate() != null, DrugwarehouseEntity::getWarehousingdate, entity.getWarehousingdate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getUsersId()), DrugwarehouseEntity::getUsersId, entity.getUsersId());
        lambdaQuery.eq(entity.getApprovalStatus() != null, DrugwarehouseEntity::getApprovalStatus, entity.getApprovalStatus());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getReviewer()), DrugwarehouseEntity::getReviewer, entity.getReviewer());
        lambdaQuery.eq(entity.getGmtCreate() != null, DrugwarehouseEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, DrugwarehouseEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<DrugwarehouseEntity> buildQuery(DrugwarehouseVO entity) {
        QueryWrapper<DrugwarehouseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(entity.getId() != null, "t1.id", entity.getId());
        wrapper.eq(entity.getDeliveryId() != null, "t1.delivery_id", entity.getDeliveryId());
        wrapper.like(StringUtils.isNotBlank(entity.getDeliveryShelflevelId()), "t1.delivery_shelflevel_id", entity.getDeliveryShelflevelId());
        wrapper.like(StringUtils.isNotBlank(entity.getShelflevelname()), "t6.sname", entity.getShelflevelname());
        wrapper.eq(entity.getStorehouseId() != null, "t1.storehouse_id", entity.getStorehouseId());
        wrapper.like(StringUtils.isNotBlank(entity.getSname()), "t2.sname", entity.getSname());
        wrapper.eq(entity.getMedicineId() != null, "t1.medicine_id", entity.getMedicineId());
        wrapper.like(StringUtils.isNotBlank(entity.getMname()), "t3.name", entity.getMname());
        wrapper.like(StringUtils.isNotBlank(entity.getWarehousingmethod()), "t1.warehousingmethod", entity.getWarehousingmethod());
        wrapper.eq(entity.getWarehousingdate() != null, "t1.warehousingdate", entity.getWarehousingdate());
        wrapper.like(StringUtils.isNotBlank(entity.getUsersId()), "t1.users_id", entity.getUsersId());
        wrapper.like(StringUtils.isNotBlank(entity.getUserName()), "t5.name", entity.getUserName());
        wrapper.eq(entity.getApprovalStatus() != null, "t1.approvals_tatus", entity.getApprovalStatus());
        wrapper.like(StringUtils.isNotBlank(entity.getReviewer()), "t1.reviewer", entity.getReviewer());
        wrapper.like(StringUtils.isNotBlank(entity.getReviewerName()), "t4.name", entity.getReviewerName());
        return wrapper;
    }

}
